package com.drinks.yiniang.adapter.user.invoice;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.adapter.shop.MfrsExpertAdapter;
import com.drinks.yiniang.common.recycler.decoration.InvoiceGoodsItemSpacesDecoration;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.entity.shop.MfrsDetailJson;
import com.drinks.yiniang.entity.user.invoice.InvoiceOrderListJson;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发票订单列表adapter
 * @author han
 * @date 2019/8/30
 */
public class InvoiceListAdapter extends BaseQuickRCVAdapter<InvoiceOrderListJson.InvoiceOrderList, BaseViewHolder> {

    private BaseRecyclerView rcv_invoice_list_goods;

    private InvoiceGoodsAdapter mInvoiceGoodsAdapter;

    private SpannableStringBuilder mBuilder;

    private Drawable mCheckedDrawable;
    private Drawable mNormalDrawable;

    private int sp18;
    private int start, end;
    private int colorPrice;

    public InvoiceListAdapter(Context mContext, @Nullable List<InvoiceOrderListJson.InvoiceOrderList> data) {
        super(R.layout.item_invoice_list, data);

        mBuilder = new SpannableStringBuilder();

        mCheckedDrawable = ContextCompat.getDrawable(mContext, R.mipmap.picker_orignal_checked);
        mNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.picker_orignal_normal);

        sp18 = ScreenSizeUtils.sp2Px(mContext, 18);

        colorPrice = ColorUtils.getColorById(mContext, R.color.color_282829);
    }

    @Override
    protected void convert(BaseViewHolder helper, InvoiceOrderListJson.InvoiceOrderList item) {
        //关联商品
        setRelatedGoodsData(helper, item.getImages());
        //总数
        helper.setText(R.id.tv_invoice_list_total_num, String.format(mContext.getString(R.string.text_total_piece_format), StringUtils.getNullEmptyConvert__(item.getNum())));
        //合计
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append("共");
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getNum()));
        mBuilder.append("件商品 合计：¥");
        start = mBuilder.length();
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getPay_amount()));
        end = mBuilder.length();
        mBuilder.setSpan(new AbsoluteSizeSpan(sp18), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new ForegroundColorSpan(colorPrice), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_invoice_list_total_money, mBuilder);

        //选中/未选中
        TextView tvCheck = helper.getView(R.id.tv_invoice_list_check);

        if(item.isChecked()) {
            tvCheck.setCompoundDrawablesWithIntrinsicBounds(mCheckedDrawable, null, null, null);
        } else {
            tvCheck.setCompoundDrawablesWithIntrinsicBounds(mNormalDrawable, null, null, null);
        }

        helper.addOnClickListener(R.id.tv_invoice_list_check);
    }

    //设置发票关联商品
    private void setRelatedGoodsData(BaseViewHolder helper, List<String> images) {
        if(images == null) {
            images = new ArrayList<>();
        }

        rcv_invoice_list_goods = helper.getView(R.id.rcv_invoice_list_goods);

        rcv_invoice_list_goods.setLayoutManager(RecyclerUtils.getHorizontalLinearLayout(mContext));
        //rcv_invoice_list_goods.addItemDecoration(new InvoiceGoodsItemSpacesDecoration(5));

        mInvoiceGoodsAdapter = new InvoiceGoodsAdapter(mContext, new ArrayList<String>());
        mInvoiceGoodsAdapter.bindToRecyclerView(rcv_invoice_list_goods);
        mInvoiceGoodsAdapter.addData(images);
    }
}
